package leetcode.code013;

import java.util.HashMap;
import java.util.Map;

public class Solution1 implements IRomanToInteger {
	public static void main(String[] args) {
		String case1 = "III";
		String case2 = "IV";
		String case3 = "IX";
		String case4 = "LVIII";
		String case5 = "MCMXCIV";
		String case6 = "D";

		int result1 = 3;
		int result2 = 4;
		int result3 = 9;
		int result4 = 58;
		int result5 = 1994;
		int result6 = 500;

		myPrint(case1, result1, new Solution1());
		myPrint(case2, result2, new Solution1());
		myPrint(case3, result3, new Solution1());
		myPrint(case4, result4, new Solution1());
		myPrint(case5, result5, new Solution1());
		myPrint(case6, result6, new Solution1());
	}

	public static void myPrint(String s, int expect, IRomanToInteger isolution) {
		int result = isolution.romanToInt(s);
		System.out.println("字符串：" + s + " 预期：" + expect + " 结果：" + result + " " + (expect == result));
	}

	@Override
	public int romanToInt(String s) {
		Map<Character, Integer> map = new HashMap<>();
		map.put('I', 1);
		map.put('V', 5);
		map.put('X', 10);
		map.put('L', 50);
		map.put('C', 100);
		map.put('D', 500);
		map.put('M', 1000);
		if (s.length() == 1) {
			return map.get(s.charAt(0));
		}
		char[] src = s.toCharArray();
		int point2 = 0;
		int result = 0;
		for (int point1 = s.length() - 1; point1 > -1; point1--) {
			point2 = point1 - 1;
			if (point2 >= 0) {
				int int1 = map.get(src[point1]);
				int int2 = map.get(src[point2]);
				if (int2 >= int1) {
					result += int1;
				} else {
					result += (int1 - int2);
					point1--;
				}
			} else {
				result += map.get(src[point1]);
			}
		}
		return result;
	}

}
